package com.joyxy.easy.gui;

import com.joyxy.easy.entity.WindowContext;
import com.joyxy.easy.manger.WindowManager;
import com.joyxy.easy.monitor.StatusMonitor;

import javax.swing.*;
import java.awt.*;

public class StatusPanel extends JPanel {
    private final long hwnd;
    private final JLabel hpLabel = new JLabel("血量: -");
    private final JLabel mpLabel = new JLabel("魔法值: -");
    private final JLabel posLabel = new JLabel("位置: (-,-)");
    private final JLabel mapLabel = new JLabel("地图: -");
    public StatusPanel(long hwnd) {
        this.hwnd = hwnd;
        setLayout(new GridLayout(4, 1));

        add(new JLabel("窗口句柄: " + hwnd));
        add(hpLabel);
        add(posLabel);
        add(mapLabel);

        // 启动监控
        WindowManager.bindWindow(hwnd);
        WindowContext context = WindowManager.getContext(hwnd);
        context.startMonitoring();

        // 定时更新UI
        new Timer(200, e -> updateStats()).start();
    }

    private void updateStats() {
        WindowContext context = WindowManager.getContext(hwnd);
        if (context != null) {
            StatusMonitor monitor = context.getMonitor();
            hpLabel.setText(String.format("HP: %.1f", monitor.getHp()));
            mpLabel.setText(String.format("MP: %.1f", monitor.getMp()));
            posLabel.setText(String.format("Position: (%d,%d)", monitor.getX(), monitor.getY()));
            mapLabel.setText(monitor.getMap() + "");
        }
    }
}